<!DOCTYPE html>
<html>
    <head>
        <title>VNC Playback</title>
    </head>
    <body>

        Iterations: <input id='iterations' style='width:50'>&nbsp;
        Perftest:<input type='radio' id='mode1' name='mode' checked>&nbsp;
        Realtime:<input type='radio' id='mode2' name='mode'>&nbsp;&nbsp;

        <input id='startButton' type='button' value='Start' style='width:100px'
            onclick="start();" disabled>&nbsp;

        <br><br>

        Results:<br>
        <textarea id="messages" style="font-size: 9;" cols=80 rows=25></textarea>

        <br><br>

        <div id="VNC_screen">
            <div id="VNC_status_bar" class="VNC_status_bar" style="margin-top: 0px;">
                <table border=0 width=100%><tr>
                    <td><div id="VNC_status">Loading</div></td>
                </tr></table>
            </div>
            <canvas id="VNC_canvas" width="640px" height="20px">
                Canvas not supported.
            </canvas>
        </div>

    </body>

    <!--
    <script type='text/javascript' 
        src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
    -->

    <script type="text/javascript">
        var INCLUDE_URI= "../include/";
        // TODO: Data file should override
        var VNC_frame_encoding = "base64";
    </script>
    <script src="../include/util.js"></script>
    <script src="../include/webutil.js"></script>

    <script>
        var fname, start_time;

        function message(str) {
            console.log(str);
            var cell = $D('messages');
            cell.innerHTML += str + "\n";
            cell.scrollTop = cell.scrollHeight;
        }

        fname = WebUtil.getQueryVar('data', null);
        if (fname) {
            message("Loading " + fname);
            // Load supporting scripts
            Util.load_scripts(["base64.js", "websock.js", "des.js",
                            "keysymdef.js", "keyboard.js", "input.js", "display.js",
                            "jsunzip.js", "rfb.js", "playback.js", fname]);

        } else {
            message("Must specify data=FOO in query string.");
        }

        updateState = function (rfb, state, oldstate, msg) {
            switch (state) {
                case 'failed':
                case 'fatal':
                    message("noVNC sent '" + state + "' state during iteration " + iteration + " frame " + frame_idx);
                    test_state = 'failed';
                    break;
                case 'loaded':
                    $D('startButton').disabled = false;
                    break;
            }
            if (typeof msg !== 'undefined') {
                $D('VNC_status').innerHTML = msg;
            }
        }

        function start() {
            $D('startButton').value = "Running";
            $D('startButton').disabled = true;

            iterations = $D('iterations').value;
            iteration = 0;
            start_time = (new Date()).getTime();

            if ($D('mode1').checked) {
                message("Starting performance playback (fullspeed) [" + iterations + " iteration(s)]");
                mode = 'perftest';
            } else {
                message("Starting realtime playback [" + iterations + " iteration(s)]");
                mode = 'realtime';
            }

            recv_message = rfb.testMode(send_array, VNC_frame_encoding);
            next_iteration();
        }

        function finish() {
            // Finished with all iterations
            var total_time, end_time = (new Date()).getTime();
            total_time = end_time - start_time;

            iter_time = parseInt(total_time / iterations, 10);
            message(iterations + " iterations took " + total_time + "ms, " +
                    iter_time + "ms per iteration");
            // Shut-off event interception
            rfb.get_mouse().ungrab();
            rfb.get_keyboard().ungrab();
            $D('startButton').disabled = false;
            $D('startButton').value = "Start";

        }

        window.onscriptsload = function () {
            iterations = WebUtil.getQueryVar('iterations', 3);
            $D('iterations').value = iterations;
            mode = WebUtil.getQueryVar('mode', 3);
            if (mode === 'realtime') {
                $D('mode2').checked = true;
            } else {
                $D('mode1').checked = true;
            }
            if (fname) {
                message("VNC_frame_data.length: " + VNC_frame_data.length);
                rfb = new RFB({'target': $D('VNC_canvas'),
                               'onUpdateState': updateState});
            }
        }
    </script>
</html>
